/*
 * Copyright (c) 2008 CCB 
 * All rights reserved.
 *
 * project: aps3.0
 * create: 2008-9-18 ����05:38:02
 * cvs: $Id: Configuration.java,v 1.1 2010/11/24 07:46:12 dev_cvs Exp $
 */
package cn.batch.daemon;

import cn.batch.daemon.exception.ConfigException;
import cn.batch.daemon.util.Assert;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;

/**
 * 为控制台提供一些配置属性
 * 
 * @author liuning
 * @version $Revision: 1.1 $
 */
public class Configuration implements Serializable {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(Configuration.class);

	private Properties settings;

	private transient ClassLoader classLoader = getDefaultClassLoader();

	/**
	 * 创建 Configuration 实例。
	 */
	public Configuration() {
		settings = new Properties();

		try {
			InputStream is = classLoader
					.getResourceAsStream("watchProcessConfig.properties");
			if (is != null) {
				settings.load(is);
			}
		} catch (Throwable ex) {
			logger.error("Cannot load watchProcessConfig.properties", ex);
			throw new ConfigException(
					"不能加载配置文件： watchProcessConfig.properties", ex);
		}
	}

	/**
	 * 使用一个初始化配置属性 Properties 创建 TaskConfiguration 实例。
	 * 
	 * @param properties
	 *            初始化配置属性
	 */
	public Configuration(Properties properties) {
		this();
		addProperties(properties);
	}

	/**
	 * 得到一个默认的类加载器（不可为 <code>null</code>）。
	 * 
	 * @return java.lang.ClassLoader
	 */
	public static ClassLoader getDefaultClassLoader() {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		} catch (Throwable ex) {
			logger.debug(
					"Cannot access thread context ClassLoader - falling back to system class loader",
					ex);
		}
		if (cl == null) {
			// 没有线程上下文类装载器 -> 使用当前类的类加载器
			cl = Configuration.class.getClassLoader();
		}
		return cl;
	}

	/**
	 * 为配置增加属性。
	 * 
	 * @param properties
	 * @return 当前的 {@link Configuration}
	 */
	public Configuration addProperties(Properties properties) {
		Assert.notNull(properties);
		settings.putAll(properties);
		return this;
	}

	/**
	 * @param setting
	 * @param defaultValue
	 * @return java.lang.String
	 */
	public String getSetting(String setting, String defaultValue) {
		String s = settings.getProperty(setting);
		if (s == null) {
			return defaultValue;
		}
		return s;
	}

	/**
	 * @param setting
	 * @param defaultValue
	 * @return int
	 */
	public int getSetting(String setting, int defaultValue) {
		return Integer.parseInt(getSetting(setting,
				String.valueOf(defaultValue)));
	}

	/**
	 * @param setting
	 * @param defaultValue
	 * @return boolean
	 */
	public boolean getSetting(String setting, boolean defaultValue) {
		return Boolean.getBoolean(getSetting(setting,
				String.valueOf(defaultValue)));
	}

	/**
	 * @param setting
	 * @param defaultClass
	 * @return java.lang.Class
	 */
	public Class<?> getSetting(String setting, Class<?> defaultClass) {
		String className = getSetting(setting, defaultClass.getName());
		try {
			return this.getClassLoader().loadClass(className);
		} catch (ClassNotFoundException e) {
			return defaultClass;
		}
	}

	/**
	 * 获取配置属性 Properties
	 * 
	 * @return 配置属性 Properties
	 */
	public Properties getProperties() {
		return settings;
	}

	/**
	 * @return 一个 ClassLoader
	 */
	public ClassLoader getClassLoader() {
		return classLoader;
	}

	/**
	 * @param classLoader
	 */
	public void setClassLoader(ClassLoader classLoader) {
		Assert.notNull(classLoader);
		this.classLoader = classLoader;
	}
}
